﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using LantingConnect.Properties;
using LantingConnect.DBModel;

namespace LantingConnect
{
   public class DbContext
    {
        //注意：不能写成静态的，不能写成静态的
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
        public SimpleClient<t_icitem> icitemDb { get { return new SimpleClient<t_icitem>(Db); } }//用来处理tb_Code表的常用操作
        public SimpleClient<SEOrder> seOrderDb { get { return new SimpleClient<SEOrder>(Db); } }//用来处理SEOrder表的常用操作

        public DbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server=" + Settings.Default.DataBaseIp + ";uid=" + Settings.Default.DBUserName
            + ";pwd=" + Settings.Default.DBPassword + ";database=" + Settings.Default.DBName,
                DbType = DbType.SqlServer,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };

            Db.CodeFirst.SetStringDefaultLength(250/*设置varchar默认长度为250*/).InitTables(typeof(t_icitem));//执行完数据库就有这个表了
            Db.CodeFirst.SetStringDefaultLength(250/*设置varchar默认长度为250*/).InitTables(typeof(SEOrder));//执行完数据库就有这个表了

        }

    }
        
}
